package com.yun.datastructure.linear.list.linkedlist.leetcode;

import com.yun.datastructure.linear.list.linkedlist.ListNode;

public class Leetcode19 {
    public ListNode removeNthFromEnd1(ListNode head, int n){
        ListNode s=new ListNode(-1,head);
        recursion(s, n);
        return s.next;
    }

    private int recursion(ListNode p,int n){
        if(p==null){
            return 0;
        }
        int nth = recursion(p.next, n);//下一个结点的倒数位置
        //System.out.println(p.val+" "+nth);
        if(nth==n){
            p.next=p.next.next;
        }
        return nth+1;
    }



    public static void main(String[] args) {
        ListNode head=ListNode.of(1,2,3,4,5);
        System.out.println(head);
        ListNode n1 = new Leetcode19().removeNthFromEnd1(head, 5);
        System.out.println(n1);
        //new E03Leetcode19().recursion(head,2);
    }
}
